package org.hyy.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hyy.dao.CategoryDAO;
import org.hyy.vo.Category;

public class CategoryAction extends BasicAction {
	private static final long serialVersionUID = -9147352318474187267L;
	private String menu;
	private List<Category> showList;
	private String pid;
	private String grade;
	private Category category;

	public Category getCategory() {
		return category;
	}

	public String getPid() {
		return pid;
	}

	public String getGrade() {
		return grade;
	}

	public List<Category> getShowList() {
		return showList;
	}

	public String getMenu() {
		return menu;
	}

	/**
	 * 显示类别下
	 * @return
	 */
	public String showCategory(){
		//获取参数
		HttpServletRequest request = getRequest();
		int menuid = Integer.parseInt(request.getParameter("menu"));
		
		//声明
		CategoryDAO categoryDAO = new CategoryDAO();
		List<Category> categories = new ArrayList<Category>();
		categories = categoryDAO.GetCatergories();
		Category category = null;
		
		if(menuid == 1){
			showList = new ArrayList<Category>();
			for(int i=0;i<categories.size();i++){
				category = new Category();
				category = categories.get(i);
				if(category.getCno()>10000&&category.getCno()<20000){
					showList.add(category);
				}
			}
			menu = "虚拟产品";
		}
		
		if(menuid == 2){
			showList = new ArrayList<Category>();
			for(int i=0;i<categories.size();i++){
				category = new Category();
				category = categories.get(i);
				if(category.getCno()>20000&&category.getCno()<30000){
					showList.add(category);
				}
			}
			menu = "电子产品";
		}
		
		if(menuid == 3){
			showList = new ArrayList<Category>();
			for(int i=0;i<categories.size();i++){
				category = new Category();
				category = categories.get(i);
				if(category.getCno()>30000&&category.getCno()<40000){
					showList.add(category);
				}
			}
			menu = "服装鞋帽";
		}
		
		if(menuid == 4){
			showList = new ArrayList<Category>();
			for(int i=0;i<categories.size();i++){
				category = new Category();
				category = categories.get(i);
				if(category.getCno()>40000&&category.getCno()<50000){
					showList.add(category);
				}
			}
			menu = "潮人地带";
		}
		
		if(menuid == 5){
			showList = new ArrayList<Category>();
			for(int i=0;i<categories.size();i++){
				category = new Category();
				category = categories.get(i);
				if(category.getCno()>50000&&category.getCno()<60000){
					showList.add(category);
				}
			}
			menu = "文体用品";
		}
		
		if(menuid == 6){
			showList = new ArrayList<Category>();
			for(int i=0;i<categories.size();i++){
				category = new Category();
				category = categories.get(i);
				if(category.getCno()>60000&&category.getCno()<70000){
					showList.add(category);
				}
			}
			menu = "家居";
		}
		
		if(menuid == 7){
			showList = new ArrayList<Category>();
			for(int i=0;i<categories.size();i++){
				category = new Category();
				category = categories.get(i);
				if(category.getCno()>70000&&category.getCno()<80000){
					showList.add(category);
				}
			}
			menu = "食品";
		}
		return "show";
	}
	
	public String showaddCategory(){
		pid = getRequest().getParameter("pid");
		grade = getRequest().getParameter("grade");
		return "showadd";
	}
	
	public String addCategory(){
		HttpServletRequest request = getRequest();
		int pid = Integer.parseInt(request.getParameter("pid"));
		int grade = Integer.parseInt(request.getParameter("grade"));

		String action = request.getParameter("action");

		if(action != null && action.trim().equals("add")) {
			String name = request.getParameter("name");
			String descr = request.getParameter("descr");
			Category c = new Category();
			c.setPid(pid);
			c.setName(name);
			c.setDescr(descr);
			c.setGrade(grade + 1);
			CategoryDAO categoryDAO = new CategoryDAO();
			categoryDAO.add(c);
		}
		return "showcategory";
	}
	
	public String showchangeCategory(){
		//获得参数
		int id = Integer.parseInt(getRequest().getParameter("id"));
		//声明
		category =  new Category();
		CategoryDAO categoryDAO = new CategoryDAO();

		//获取该类别的信息
		category = categoryDAO.findById(id);
		return "showchange";
	}
	
	public String changeCategory(){
		//取参数
		HttpServletRequest request = getRequest();
		String name = request.getParameter("name");
		String descr = request.getParameter("descr");
		int id = Integer.parseInt(request.getParameter("id"));
		
		//声明
		CategoryDAO categoryDAO = new CategoryDAO();
		categoryDAO.change(id, name, descr);
		return "showcategory";
	}
}
